const Router = require('koa-router');
const apyCtl= require('../../controllers/awayphys.js')
const router = new Router();

/**
 * @swagger
 * tags:
 *   - name: competition
 *     description: Everything about competition
 */

router
/**
 * @swagger
 * paths:
 *   /competitions:
 *     get:
 *       tags:
 *         - competition
 *       summary: Return a list of competitions
 *       description: ''
 *       produces:
 *         - application/json
 *       responses:
 *         200:
 *           description: OK
 *           schema:
 *             type: array
 *             items:
 *               $ref: '#/definitions/Competition'
 */
    .get('/', async (ctx, next)=>{ ctx.body = await apyCtl.list(); })

/**
 * @swagger
 * paths:
 *   /competitions:
 *     post:
 *       tags:
 *         - competition
 *       summary: Create a competition
 *       description: ''
 *       parameters:
 *         - in: body
 *           name: "body"
 *           description: ""
 *           required: true
 *           schema:
 *             $ref: "#/definitions/Competition"
 *       produces:
 *         - application/json
 *       responses:
 *         200:
 *           description: OK
 */
    .post('/', async (ctx, next)=>{
        const {id} = ctx.params;
        const data = ctx.request.body;
        for(let key of Object.keys(data)){
            let a = data[key]
            const  r = await apyCtl.create(a);
            //console.log(a)
        }
        /**for (let a of data){
            //const  r = await cmptCtl.create(a);
            console.log(a)
        }*/
        //ctx.body = r;
    })
/**
 * @swagger
 * paths:
 *   /competitions/{cid}:
 *     get:
 *       tags:
 *         - competition
 *       summary: Return a competition
 *       description: ''
 *       parameters:
 *         - in: path
 *           name: cid 
 *           description: ""
 *           required: true
 *           type: string
 *       produces:
 *         - application/json
 *       responses:
 *         200:
 *           description: OK
 *           schema:
 *             $ref: '#/definitions/Competition'
 */
    .get('/:id', async (ctx, next)=>{
        // Retrieves informations about a given competition
        const {id} = ctx.params;
        const r = await apyCtl.retrieve(id);
        ctx.assert(r !== null, 404, 'Detail not found!');

        ctx.body = r;
    })

/**
 * @swagger
 * paths:
 *   /competitions/{cid}:
 *     put:
 *       tags:
 *         - competition
 *       summary: Update or create a competition
 *       description: ""
 *       parameters:
 *         - in: path
 *           name: cid 
 *           description: ""
 *           required: true
 *           type: string
 *         - in: body
 *           name: body
 *           description: ""
 *           required: true
 *           schema:
 *             $ref: "#/definitions/Competition"
 *
 *       produces:
 *         - application/json
 *       responses:
 *         200:
 *           description: OK
 */
    .put('/:id', async (ctx, next)=>{
        const id = ctx.params.id;
        const data = ctx.request.body;
        const r = await apyCtl.upsert(id, data);
        
        ctx.body = r;
    })

/**
 * @swagger
 * paths:
 *   /competitions/{cid}:
 *     delete:
 *       tags:
 *         - competition
 *       summary: Delete a competition
 *       description: ""
 *       parameters:
 *         - in: path
 *           name: cid 
 *           description: ""
 *           required: true
 *           type: string
 *
 *       produces:
 *         - application/json
 *       responses:
 *         204:
 *           description: No content
 */
    .delete('/:id', async (ctx, next)=>{
        apyCtl.removeById(ctx.params.id);
        ctx.status = 204;
    })
/**
 * @swagger
 * paths:
 *   /competitions/{cid}/seasons:
 *     get:
 *       tags:
 *         - competition
 *       summary: Return the list of seasons of the given competition
 *       description: ""
 *       parameters:
 *         - in: path
 *           name: cid 
 *           description: ""
 *           required: true
 *           type: string
 *
 *       produces:
 *         - application/json
 *       responses:
 *         200:
 *           description: OK
 */
    .get('/:id/seasons', async (ctx, next)=>{
        const {id} = ctx.params;
        ctx.body = "TODO find the list of seasons of competition" + id;
        ctx.throw(500);
    })

    .get('/time/list/:time', async (ctx, next)=>{
        // Retrieves informations about a given competition
        const {time} = ctx.params;
        const r = await apyCtl.find({time: time});
        //console.log(r)
        //ctx.assert(r !== null, 404, 'Detail not found!');
        ctx.body = r;
    })

module.exports = router;

/**
 * @swagger
 * resourcePath: /api
 * description: All about API
 * definitions:
 *   Competition:
 *     type: "object"
 *     required:
 *       - "name"
 *     properties:
 *       id:
 *         type: "string"
 *       name:
 *         type: "string"
 *       area:
 *         type: "string"
 *       foramt:
 *         type: "string"
 *         enum:
 *           - "Domestic cup"
 *           - "Domestic league"
 *           - "Domestic super cup"
 *           - "International cup"
 *           - "International super cup"
 *       type:
 *         type: "string"
 *         enum:
 *          - club
 *          - inernational
 *       categroy: 
 *         type: "string"
 *         enum:
 *          - defalut
 *          - U12
 *          - U13
 *          - U14
 *          - U15
 *          - U16
 *          - U17
 *          - U18
 *          - U19
 *          - U20
 *          - U21
 *          - U22
 *          - U23
 *       gender: 
 *         type: "string"
 *         enum:
 *           - male
 *           - female
 *       divisionLevel:
 *         type: "number"
 *         enum:
 *           - 0
 *           - 1
 *           - 2
 *           - 3
 *           - 4
 *           - 5
 */